// pages/info/info.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    location: 'loading...',
    provinceName: 'loading...',
    weatherDate: 'loading...',
    listArray: [],
    weatherArray: [],
    weatherPerHour: [],
    code: null,
    daily: [],
    nowair: {},
    weatherdate: '',
    sunrisetime: 'N/A',
    sunsettime: 'N/A',
    moonrisetime: 'N/A',
    moonsettime: 'N/A'

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.setData({
      location: options.location,
      provinceName: options.provinceName,
      listArray: JSON.parse(options.listArray),
      weatherArray: JSON.parse(options.weatherArray),
      weatherPerHour: that.remapData(JSON.parse(options.weatherArray)[0].WeatherPerHour),
      weatherDate: JSON.parse(options.weatherArray)[0].WeatherDate,
      code: JSON.parse(options.weatherArray)[0].Code,
      weatherdate: that.clear(JSON.parse(options.weatherArray)[0].WeatherDate)
    });

    wx.request({
      url: 'https://devapi.qweather.com/v7/indices/1d?type=8,2,3,9,1,6,5,10&location=' + that.data.code + '&key=1989b40e90a2420f8969a4ed455dbf65',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        // console.log(res.data)
        that.setData({
          daily: res.data.daily
        })
      }
    });

    wx.request({
      url: 'https://devapi.qweather.com/v7/air/now?location=' + that.data.code + '&key=1989b40e90a2420f8969a4ed455dbf65',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        // console.log(res.data)
        that.setData({
          nowair: res.data.now
        })
      }
    });

    wx.request({
      url: 'https://devapi.qweather.com/v7/astronomy/sun?location=' + that.data.code + '&date=' + that.data.weatherdate + '&key=1989b40e90a2420f8969a4ed455dbf65',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        // console.log(res.data)
        that.setData({
          sunrisetime: that.cleardate(res.data.sunrise),
          sunsettime: that.cleardate(res.data.sunset)
        })
      }
    });

    wx.request({
      url: 'https://devapi.qweather.com/v7/astronomy/moon?location=' + that.data.code + '&date=' + that.data.weatherdate + '&key=1989b40e90a2420f8969a4ed455dbf65',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        // console.log(res.data)
        that.setData({
          moonrisetime: that.cleardate(res.data.moonrise),
          moonsettime: that.cleardate(res.data.moonset)
        })
      }
    });
  },

  remapData(data) {
    let listData = [];
    for (let i = 0; i < data.length; i++) {
      data[i].icon = this.getWeatherIcon(data[i].WeatherInfo);
      listData.push(data[i])
    }
    this.setData({
      weatherPerHour: listData,
    });

    return data;
  },

  getWeatherIcon(weather) {
    switch (weather) {
      case "晴":
        return "/images/icons/weather_icon_4.svg";
      case "多云":
        return "/images/icons/weather_icon_3.svg";
      case "阴":
        return "/images/icons/weather_icon_2.svg";
      case "阵雨":
        return "/images/icons/weather_icon_8.svg";
      case "雷阵雨":
        return "/images/icons/weather_icon_23.svg";
      case "小雨":
        return "/images/icons/weather_icon_13.svg";
      case "中雨":
        return "/images/icons/weather_icon_7.svg";
      case "大雨":
        return "/images/icons/weather_icon_16.svg";
      case "阵雪":
        return "/images/icons/weather_icon_36.svg";
      case "小雪":
        return "/images/icons/weather_icon_35.svg";
      case "中雪":
        return "/images/icons/weather_icon_28.svg";
      case "大雪":
        return "/images/icons/weather_icon_28.svg";
      case "暴雪":
        return "/images/icons/weather_icon_28.svg";
      case "雾":
        return "/images/icons/weather_icon_30.svg";
      case "晴转多云":
        return "/images/icons/weather_icon_4.svg";
      case "多云转中雨":
        return "/images/icons/weather_icon_17.svg";
      case "多云转晴":
        return "/images/icons/weather_icon_3.svg";
      case "多云转阴":
        return "/images/icons/weather_icon_3.svg";
      case "中雨转多云":
        return "/images/icons/weather_icon_8.svg";
      case "晴转多云":
        return "/images/icons/weather_icon_3.svg";
      case "阴转多云":
        return "/images/icons/weather_icon_2.svg";
      case "阴转晴":
        return "/images/icons/weather_icon_2.svg";
      case "阴转小雨":
        return "/images/icons/weather_icon_2.svg";
      case "多云转小雨":
        return "/images/icons/weather_icon_14.svg";
      case "小雨转多云":
        return "/images/icons/weather_icon_13.svg";
      case "雷阵雨转多云":
        return "/images/icons/weather_icon_24.svg"
        break;
    }
  },

  clear(data) {
    let reg = new RegExp("-", "g");
    let a = data.replace(reg, "");
    return a;

  },

  cleardate(data) {
    let a = data.substr(11, 5);
    return a;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})